package ta.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import ta.dao.BlogDao;
import ta.dao.CategoryDao;
import ta.pojo.Blog;
import ta.pojo.Category;
import ta.utils.PageInfo;

@WebServlet("/Index")
public class Index extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type=request.getParameter("type");
		switch (type) {
		case "getlist":
			getList(request,response);
			break;

		case "getlist1":
			getList1(request,response);
			break;

		case "getlist2":
			getList2(request,response);
			break;

		case "add1":
			add1(request,response);
			break;

		case "getblog":
				getBlog(request,response);
			break;
		}
	}

	protected void add1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String[]> map = request.getParameterMap();
		Blog blog = new Blog();
		try
		{
			BeanUtils.populate(blog, map);
		} catch (IllegalAccessException | InvocationTargetException e)
		{
			e.printStackTrace();
		}
		blog.setAuthor((String)request.getSession().getAttribute("loginame"));
		LocalDateTime date=LocalDateTime.now();
		DateTimeFormatter sdf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		blog.setPubtime(date.format(sdf));
		BlogDao dao = new BlogDao();
		dao.add(blog);
		// 跳转到页面
		response.sendRedirect("Index?type=getlist");
	}

	protected void getList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		BlogDao dao = new BlogDao();
		List<Blog> blogAll= dao.getAll();
		CategoryDao dao1=new CategoryDao();
		List<Category>  list =dao1.getAll();
		request.setAttribute("list", list);
		request.setAttribute("blogAll", blogAll);
		request.getRequestDispatcher("indexlogin.jsp").forward(request, response);
	}
	protected void getList1(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		BlogDao dao = new BlogDao();
		List<Blog> blogAll= dao.getAll();
		CategoryDao dao1=new CategoryDao();
		List<Category>  list =dao1.getAll();
		request.setAttribute("list", list);
		request.setAttribute("blogAll", blogAll);
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}
	protected void getList2(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keyword=request.getParameter("keyword");
		//Blog blog=new Blog();
		//blog.setAuthor((String)request.getSession().getAttribute("loginame"));
		BlogDao dao = new BlogDao();
		List<Blog> blogByAuthor= dao.getAll(keyword);
		CategoryDao dao1=new CategoryDao();
		List<Category>  list =dao1.getAll();
		request.setAttribute("list", list);
		request.setAttribute("blogByAuthor", blogByAuthor);
		request.setAttribute("keyword",keyword);
		request.getRequestDispatcher("personalPage.jsp").forward(request, response);
	}

	protected void getBlog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String blogid=request.getParameter("blogid");
		BlogDao dao=new BlogDao ();
		Blog blog= dao.getById(blogid);
		request.setAttribute("blog", blog);
		request.getRequestDispatcher("details.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
